package com.china08.yxyapi.api.manage.edu.platform.continuedu.renwu;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduLesson;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTaskLesson;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskLessonRepository;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyContinueduRenWuCrtController extends YxyApiBaseController {

    @Autowired
    YxyContinueduTaskRepository taskRepository;

    @Autowired
    YxyContinueduTaskLessonRepository taskLessonRepository;

    @Autowired
    YxyContinueduLessonRepository lessonRepository;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/continuedu/renwu/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) throws ParseException {
        String name = reqModel.name;
        Integer score = reqModel.score;
        Date beginDate = reqModel.beginDate;
        Date endDate = reqModel.endDate;
        List<String> lessonIds = reqModel.lessonIds;
        YxyProfile profile = getProfile(request, response);
        String schoolId = profile.getSchoolId();
        String areaCodeProvince = profile.getSchoolAreaIdProvince();
        String areaCodeCity = profile.getSchoolAreaIdCity();
        String areaCodeDistrict = profile.getSchoolAreaIdDistrict();
        // 任务进行状态
        Integer status = 0;
        // 判断时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(System.currentTimeMillis());
        Date date2 = formatter.parse(date);
        long l1 = date2.getTime();
        long l2 = beginDate.getTime();
        long l3 = endDate.getTime();
        // 系统时间==结束时间：进行中
        if (l1 == l3) {
            status = 0;
        }
        // 当前系统时间==开始时间：进行中
        else if (l1 == l2) {
            status = (0);
        }
        // 当前系统时间<开始时间: 未开始
        else if (l1 < l2) {
            status = (-1);
        }
        // 当前系统时间>开始时间，并且小于结束时间：进行中
        else if (l1 > l2 && l1 < l3) {
            status = (0);
        }
        // 当前系统时间>开始时间，并且大于结束时间：已截止
        else if (l1 > l2 && l1 > l3) {
            status = (1);
        }

        YxyContinueduTask task = new YxyContinueduTask();
        task.setName(name);
        task.setScore(score);
        task.setSchoolId(schoolId);
        task.setAreaCodeCity(areaCodeCity);
        task.setAreaCodeDistrict(areaCodeDistrict);
        task.setAreaCodeProvince(areaCodeProvince);
        task.setBeginDate(beginDate);
        task.setEndDate(endDate);
        task.setStatus(status);
        task = taskRepository.saveAndFlush(task);
        // 任务id
        String taskId = task.getId();

        // 获取排序字段
        YxyContinueduTaskLesson taskLesson = taskLessonRepository.findTop1ByTaskIdOrderByOrderNumDesc(taskId);
        Integer orderNum = 1;
        if (taskLesson != null) {
            orderNum = taskLesson.getOrderNum() + 1;
        }

        for (String lessonId : lessonIds) {
            YxyContinueduLesson lesson = lessonRepository.findById(lessonId);
            taskLesson = new YxyContinueduTaskLesson();
            taskLesson.setTaskId(taskId);
            taskLesson.setLessonId(lessonId);
            taskLesson.setLessonName(lesson.getName());
            taskLesson.setOrderNum(orderNum);
            taskLessonRepository.saveAndFlush(taskLesson);
            orderNum++;
        }
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String name;
        public Integer score;
        public Date beginDate;
        public Date endDate;
        public List<String> lessonIds;

        @Override
        public void autoCheck() {
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }
}
